package com.yanqu.road.server.gameplayer.module.activity.mammonhall;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.mammonhall.MammonHallTimeInfo;
import com.yanqu.road.entity.activity.mammonhall.MammonHallUserData;
import com.yanqu.road.entity.activity.mammonhall.MammonHallWorship;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.logic.bussiness.activity.MammonHallBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.activity.MammonHallProto;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.base.GeneralModule;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.activity.mammonhall.MammonHallMgr;
import com.yanqu.road.server.manger.config.SystemOpenMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.date.DateHelper;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MammonHallModule extends GeneralModule {

    MammonHallUserData userData;

    public MammonHallModule(GamePlayer player) {
        super(player);
    }

    @Override
    public boolean loadData() {
        List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.PlatformActivity.getValue(), MammonHallMgr.activityChildType);
        if(activityInfoList.size() > 0){
            userData = MammonHallBusiness.getUserDataByUserId(player.getUserId(), activityInfoList.get(0).getActivityId());
        }
        return true;
    }

    @Override
    public boolean afterLoadData() {
        return false;
    }

    @Override
    public boolean saveData() {
        if(userData != null){
            if(userData.isInsertOption()){
                MammonHallBusiness.addMammonHallUserData(userData);
            }else if(userData.isUpdateOption()){
                MammonHallBusiness.updateMammonHallUserData(userData);
            }
        }
        return true;
    }

    @Override
    public void afterLogin() {
        if(userData != null){
            if(userData.getWorshipMap().size() == 0){
                synchronized (this){
                    if(userData.getWorshipMap().size() == 0) {
                        Map<Integer, MammonHallWorship> worshipMap = new HashMap<>();
                        for (MammonHallTimeInfo timeInfo : MammonHallMgr.getTimeInfoList()) {
                            MammonHallWorship worship = new MammonHallWorship();
                            worship.setType(timeInfo.getType());
                            worship.setWorship(false);
                            worshipMap.put(timeInfo.getType(), worship);
                        }
                        userData.setWorshipMap(worshipMap);
                    }
                }
            }
            resetWorship();
        }
        syncActivity();
    }

    /**
     * 初始玩家数据
     */
    public void initUserData(ActivityInfo activityInfo){
        synchronized (this){
            if(userData == null || userData.getActivityId() != activityInfo.getActivityId()){
                userData = new MammonHallUserData();
                userData.setActivityId(activityInfo.getActivityId());
                userData.setUserId(player.getUserId());
                userData.setResetTime(System.currentTimeMillis());
                for (MammonHallTimeInfo timeInfo : MammonHallMgr.getTimeInfoList()) {
                    MammonHallWorship worship = new MammonHallWorship();
                    worship.setType(timeInfo.getType());
                    worship.setWorship(false);
                    userData.getWorshipMap().put(timeInfo.getType(), worship);
                }
                userData.setInsertOption();
            }
        }
    }

    /**
     * 系统解锁
     */
    public void initSystem(){
        syncActivity();
    }

    /**
     * 下发活动相关数据
     */
    public void syncActivity() {
        if (SystemOpenMgr.systemOpen(player, eSystemId.MammonHall.getValue())) {
            List<ActivityInfo> activityInfoList = NormalActivityMgr.getOpenActivityInfoList(eActivityType.PlatformActivity.getValue(), MammonHallMgr.activityChildType);
            if(activityInfoList.size() > 0){
                ActivityInfo activityInfo = activityInfoList.get(0);
                if(userData == null || userData.getActivityId() != activityInfo.getActivityId()){
                    initUserData(activityInfo);
                }
                player.sendPacket(Protocol.U_MAMMON_HALL_USER_DATA_SYNC, MammonHallMgr.getUserDataSyncBuilder(userData));
            }else {
                userData = null;
            }
        }
    }

    /**
     * 膜拜
     */
    public int worship(int type) {
        if(userData == null){
            return GameErrorCode.E_MAMMON_HALL_SYSTEM_NO_OPEN;
        }
        if(!userData.getWorshipMap().containsKey(type)){
            return GameErrorCode.E_REQUEST_ARGUMENT_ERROR;
        }
        //时间限制
        if(!MammonHallMgr.inOpenTime(type)){
            return GameErrorCode.E_MAMMON_HALL_RANK_TYPE_NO_OPEN;
        }
        MammonHallWorship worship = userData.getWorshipMap().get(type);
        synchronized (userData) {
            if (worship.isWorship()) {
                return GameErrorCode.E_MAMMON_HALL_HAD_WORSHIP;
            }
            worship.setWorship(true);
            userData.setUpdateOption();

            //奖励
            player.getModule(CurrencyModule.class).addCurrency(GameConfig.GAME_MONEY_INGOTS + "=" + MammonHallMgr.MAMMON_HALL_RANK_WORSHIP_REWARD, eLogMoneyType.MammonHall,
                    eLogMoneyType.MammonHallWorshipReward);

            MammonHallProto.MammonHallRankWorshipRespMsg.Builder respMsg = MammonHallProto.MammonHallRankWorshipRespMsg.newBuilder();
            respMsg.setRet(0);
            respMsg.setReward(GameConfig.GAME_MONEY_INGOTS + "=" + MammonHallMgr.MAMMON_HALL_RANK_WORSHIP_REWARD);
            player.sendPacket(Protocol.U_MAMMON_HALL_RANK_WORSHIP, respMsg);
        }
        return 0;
    }

    /**
     * 每日重置
     */
    public void resetOneDay(){
        if(userData == null){
            return;
        }
        resetWorship();
        syncActivity();
    }

    /**
     * 重置膜拜数据
     */
    private void resetWorship() {
        long zeroTime = DateHelper.getTodayZeroTimeStamp();
        if(zeroTime > userData.getResetTime()){
            synchronized (userData){
                for (Map.Entry<Integer, MammonHallWorship> worshipEntry : userData.getWorshipMap().entrySet()) {
                    worshipEntry.getValue().setWorship(false);
                }
                userData.setResetTime(zeroTime);
                userData.setUpdateOption();
            }
        }
    }
}
